﻿using ICSharpCode.AvalonEdit.Document;
using Org.BouncyCastle.Asn1.Mozilla;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    class TreeListTextLine
    {
        public DocumentLine Line { get; set; }
        public char[] HeaderTextArray { get; set; }
        public string TailText { get; set; }
        public string OldText { get; set; }
        public string NewText { get; set; }
        public int OldLevel { get; set; }
        public int NewLevel { get; set; }

        public List<TreeListTextLine> Children = new List<TreeListTextLine>();

        private double? number = null;
        /// <summary>
        /// 注意：这个属性的值应该与有无子级有关。
        /// 如果没有下级，直接返回自身的 number；
        /// 如果有下级，返回下级 Number 的和。
        /// </summary>
        public double? Number
        {
            get
            {
                if (HasChildren == false) return number;

                double sum = 0;
                foreach (var child in Children)
                {
                    if (child.Number.HasValue)
                        sum += child.Number.Value;
                }
                return sum;
            }
            set
            {
                number = value;
            }
        }

        public bool HasChildren
        {
            get { return Children.Count > 0; }
        }

        internal static void FindChildren(ref List<TreeListTextLine> allLines)
        {
            // 先找出所有直接下级
            for (int m = 0; m < allLines.Count; m++)
            {
                var tm = allLines[m];
                for (int n = m + 1; n < allLines.Count; n++)
                {
                    var tn = allLines[n];
                    if (tn.NewLevel >= tm.NewLevel + 1)  // 孙子级不能 break;
                    {
                        if (tn.NewLevel == tm.NewLevel + 1)  // 只需要直接子级
                        {
                            tm.Children.Add(tn);
                        }
                    }
                    else break;
                }
            }
        }

        internal static void Aggregate(ref List<TreeListTextLine> allLines)
        {
            if (allLines.Count <= 1) return;     //没有汇总的必要

            FindChildren(ref allLines);  // 找出所有直接下级，间接的不要

            // 格式化所有数字
            Regex reg = new Regex(@"[=＝][ 　\t]{0,}?[\{\[【｛［].*?[\}\]】］｝][ 　\t]{0,}?$");

            // 给所有没有下级的行取出数字值
            for (int i = 0; i < allLines.Count; i++)
            {
                var ti = allLines[i];

                if (ti.HasChildren == false)
                {
                    var matchOfNumber = reg.Match(ti.TailText);
                    if (matchOfNumber.Success)
                    {
                        if (double.TryParse(matchOfNumber.Value.Trim(new char[] { ' ', '　', '\t', '=', '＝', '{', '}', '[', ']', '【', '】', '｛', '｝', '［', '］', }), out double num))
                        {
                            ti.Number = num;
                        }
                    }
                }
            }

            var trimChars = new char[] { ' ', '　', '\t', };

            // 格式化数字文本
            for (int i = 0; i < allLines.Count; i++)
            {
                var ti = allLines[i];

                if (ti.HasChildren == false)
                {
                    var matchOfNumber = reg.Match(ti.TailText);
                    if (matchOfNumber.Success)
                    {
                        if (double.TryParse(matchOfNumber.Value.Trim(new char[] { ' ', '　', '\t', '=', '＝', '{', '}', '[', ']', '【', '】', '｛', '｝', '［', '］', }), out double num))
                        {
                            ti.TailText = ti.TailText.Substring(0, matchOfNumber.Index).Trim(trimChars) + " = [" + num.ToString() + "]";
                        }
                    }
                    continue;
                }

                if (ti.Number.HasValue)
                {
                    var match = reg.Match(ti.TailText);
                    if (match.Success)
                    {
                        ti.TailText = ti.TailText.Substring(0, match.Index).Trim(trimChars) + " = {" + ti.Number.ToString() + "}";
                    }
                }
            }
        }
    }
}
